package Domain;

import org.apache.log4j.Logger;
import org.w3c.dom.*;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;
import java.util.StringTokenizer;

import Domain.DataStructures.IMovieData;

/**
 * This class contains all the information about a movie in the system
 */
public class Movie implements IMovieData,Serializable {

    static Logger logger            = Logger.getLogger("MovieSystem.Domain.Movie");

    private String m_name;   //this field is the Movie ID
    private String m_director;
    private String m_year;
    private int m_length;
    private String m_country;
    private LinkedList<Genre> m_genres;
    private LinkedList<Actor> m_actors;

    private static final int DEFAULT_RANK = 5;
    
    public Movie(String m_name, String m_director, String m_year, int m_length, String m_country) {
        this.m_name = m_name.toLowerCase().trim();
        this.m_director = m_director.toLowerCase().trim();
        this.m_year = m_year.trim();
        this.m_length = m_length;
        this.m_country = m_country.toLowerCase().trim();
        this.m_genres = new LinkedList<Genre>();
        this.m_actors = new LinkedList<Actor>();
    }

    public Movie(String m_name, String m_director, String m_year, int m_length, String m_country, LinkedList<Integer> m_genres, LinkedList<Integer> m_actors) {
        this(m_name, m_director, m_year, m_length, m_country);
        // TODO : make it work
        //for (Integer genreId : m_genres){
            //this.m_genres.add(MySql);
        //}
        //for (Integer actorId : m_actors){
            //this.m_actors.add(new Actor(actor[0],actor[1]));
        //}
    }


    public String getName() {
        return m_name;
    }

    public String getDirector() {
        return m_director;
    }

    public String getYear() {
        return m_year;
    }

    public int getLength() {
        return m_length;
    }

    public String getCountry() {
        return m_country;
    }

    public LinkedList<Genre> getGenres() {
        return m_genres;
    }

    public LinkedList<Actor> getActors() {
        return m_actors;
    }
    
    public void addActor(Actor a){
        m_actors.add(a);
    }

    public void addGenre(Genre g){
        m_genres.add(g);
    }

    public int getDefaultRank() {
        return DEFAULT_RANK;
    }

    public boolean isMatch(String keyword, SearchMovieMethods method) {
        boolean res;
        switch(method){
            case EXACT_NAME:
                res = this.m_name.equals(keyword);      break;
            case COUNTRY:
                res = this.m_country.equals(keyword);   break;
            case YEAR:
                res = this.m_year.equals(keyword);      break;
            case DIRECTOR:
                res = this.m_director.equals(keyword);  break;
            default:
                res=false;
                logger.warn("Checking isMatch("+keyword+","+method+") for an unsupported method!");
        }
        logger.debug("Checking isMatch("+keyword+","+method+") for movie "+this.m_name+" is : "+res);
        return res;
    }
}
